package com.example.nfctagcommons.controllers;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;

import com.example.nfctagcommons.classes.VigilancePoint;
import com.example.nfctagcommons.exceptions.ConnectivityException;
import com.example.nfctagcommons.server.HttpsExecuter;
import com.example.nfctagcommons.server.ServerInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class VigilanceLocalController {

	private static final Gson gson = new Gson();
	
	public static List<VigilancePoint> getVigilanceSetAsList(Activity activity, String guardId) throws ConnectivityException{
		String completeURL = ServerInfo.SERVER_HOST+ServerInfo.SERVICES_PATH;
		String query = "operation=getVigilanceSet&guardId="+guardId;
		String response = HttpsExecuter.execute(activity, completeURL, query, true);
		try{
        	Type t = new TypeToken<List<VigilancePoint>>(){}.getType();
        	List<VigilancePoint> list = gson.fromJson(response, t);
	        return list;
        }catch (Exception x){
        	x.printStackTrace();
        	return null;
        }
	}
	
	public static HashMap<String, VigilancePoint> getVigilanceSet(Activity activity, String guardId) throws ConnectivityException{
		List<VigilancePoint> list = getVigilanceSetAsList(activity,guardId);
		try{
			HashMap<String, VigilancePoint> ret = new HashMap<String, VigilancePoint>();
			for (VigilancePoint local : list){
				ret.put(local.getLocalId(), local);
			}
			return ret;
		}catch (Exception x){
			x.printStackTrace();
			return null;
		}
	}

	public static List<VigilancePoint> getAllLocals(Activity activity) throws ConnectivityException {
		String completeURL = ServerInfo.SERVER_HOST+ServerInfo.SERVICES_PATH;
		String query = "operation=getAllLocals";
		String response = HttpsExecuter.execute(activity, completeURL, query, true);
		try{
        	Type t = new TypeToken<List<VigilancePoint>>(){}.getType();
        	List<VigilancePoint> list = gson.fromJson(response, t);
	        return list;
        }catch (Exception x){
        	x.printStackTrace();
        	return null;
        }
	}

}
